package com.example.task1.ui.user.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;

import retrofit2.Call;

public class ChangePwdActivity extends BaseActivity {

    private EditText etOldPwd;
    private EditText etNewPwd;
    private Button btnModification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        initView();
        initData();
    }

    private void initData() {
        btnModification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String olPwd = etOldPwd.getText().toString();
                String newPwd = etNewPwd.getText().toString();
                if (TextUtils.isEmpty(olPwd)) {
                    etOldPwd.setError("不能为空");
                } else if (TextUtils.isEmpty(newPwd)) {
                    etNewPwd.setError("不能为空");
                } else {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("oldPassword", olPwd);
                    map.put("newPassword", newPwd);
                    Call<ApiResp> put = Api.api().put(MyApplication.TOKEN, "/prod-api/api/common/user/resetPwd", Api.getBody(map));
                    Api.send(put, ApiResp.class, new CallBack<ApiResp>() {
                        @Override
                        public void success(ApiResp data) {
                            MyApplication.show("修改成功");
                            finish();
                        }

                        @Override
                        public void error(String msg) {

                        }
                    });
                }
            }
        });
    }

    @Override
    public String getMyTitle() {
        return "修改密码";
    }

    private void initView() {
        etOldPwd = findViewById(R.id.old_pwd);
        etNewPwd = findViewById(R.id.new_pwd);
        btnModification = findViewById(R.id.btn_modification);
    }
}